package com.freejy.ecommunity.web.controller;

import com.freejy.ecommunity.model.SysConfig;
import com.freejy.ecommunity.service.SysConfigService;
import com.freejy.ecommunity.web.dto.response.SysConfigResDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jerry
 */
@RestController
@RequestMapping("/v1")
public class SysConfigController extends BaseController
{

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * get system configs
     */
    @RequestMapping(value = "/config", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> config() throws Exception
    {
        List<SysConfigResDto> resDtos = new ArrayList<>();
        List<SysConfig> list = sysConfigService.findAll();
        if (list != null && list.size() > 0)
        {
            list.parallelStream().forEach(obj -> resDtos.add(new SysConfigResDto(obj.getName(), obj.getCategory(), obj.getValue())));
        }
        // TODO items info
        return success(resDtos);
    }

}
